#####################################################################
#                                                                   #
#  THIS IS A SOURCE CODE FILE FROM A PROGRAM TO INTERACT WITH THE   #
# LBRY PROTOCOL ( lbry.com ). IT WILL USE THE LBRY SDK ( lbrynet )  #
# FROM THEIR REPOSITORY ( https://github.com/lbryio/lbry-sdk )      #
# WHICH I GONNA PRESENT TO YOU AS A BINARY. SINCE I DID NOT DEVELOP #
# IT AND I'M LAZY TO INTEGRATE IN A MORE SMART WAY. THE SOURCE CODE #
# OF THE SDK IS AVAILABLE IN THE REPOSITORY MENTIONED ABOVE.        #
#                                                                   #
#      ALL THE CODE IN THIS REPOSITORY INCLUDING THIS FILE IS       #
# (C) J.Y.Amihud and Other Contributors 2021. EXCEPT THE LBRY SDK.  #
# YOU CAN USE THIS FILE AND ANY OTHER FILE IN THIS REPOSITORY UNDER #
# THE TERMS OF GNU GENERAL PUBLIC LICENSE VERSION 3 OR ANY LATER    #
# VERSION. TO FIND THE FULL TEXT OF THE LICENSE GO TO THE GNU.ORG   #
# WEBSITE AT ( https://www.gnu.org/licenses/gpl-3.0.html ).         #
#                                                                   #
# THE LBRY SDK IS UNFORTUNATELY UNDER THE MIT LICENSE. IF YOU ARE   #
# NOT INTENDING TO USE MY CODE AND JUST THE SDK. YOU CAN FIND IT ON #
# THEIR OFFICIAL REPOSITORY ABOVE. THEIR LICENSE CHOICE DOES NOT    #
# SPREAD ONTO THIS PROJECT. DON'T GET A FALSE ASSUMPTION THAT SINCE #
# THEY USE A PUSH-OVER LICENSE, I GONNA DO THE SAME. I'M NOT.       #
#                                                                   #
# THE LICENSE CHOSEN FOR THIS PROJECT WILL PROTECT THE 4 ESSENTIAL  #
# FREEDOMS OF THE USER FURTHER, BY NOT ALLOWING ANY WHO TO CHANGE   #
# THE LICENSE AT WILL. SO NO PROPRIETARY SOFTWARE DEVELOPER COULD   #
# TAKE THIS CODE AND MAKE THEIR USER-SUBJUGATING SOFTWARE FROM IT.  #
#                                                                   #
#####################################################################

# Preparing the executable of lbrynet to be running. If we don't that
# there will be a nasty error of permission being denied.

import os
os.system("chmod u+x flbry/lbrynet")

# I'm not trying to make it pretty. I'm trying to make it snappy.
# It's FastLBRY not SlowLBRY.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

# It's gonna take a couple of things to make the button run
from flbry import connect
from flbry import parse
from flbry import settings
from flbry import url
from flbry import ui

settings.make_sure_file_exists()

# Here I gonna set up a window

win = Gtk.Window() # Make a GTK window object
win.connect("destroy", Gtk.main_quit) # If you close the window, program quits
win.set_title("FastLBRY GTK")
win.set_default_icon_from_file("logo.png")
win.set_size_request(800, 900)
#win.maximize()

# setting up settings in memeory ( RAM )

win.settings = {}
win.settings = settings.load()

print(win.settings)

# Here I want to draw the top toolbar. But there is a catch.
# You have to split the window in two to get it working first.
# Your window can only have one widget added to it. We need a widget
# that can split the area in two. Such a widget is called BOX.

box = Gtk.VBox()
win.add(box)

pannel = Gtk.HeaderBar()
pannel.set_show_close_button(True)

# The settings in the corner.

# First we are going to start with a little POPOVER thingy.
# It will contain all secondary things.
hamburger = Gtk.Popover()
hambox = Gtk.VBox()
hamburger.add(hambox)

settings_button = Gtk.Button("Settings")
#settings_icon = ui.icon(win, "settings")
#settings_button.add(settings_icon)
settings_button.set_relief(Gtk.ReliefStyle.NONE)
settings_button.connect("clicked", settings.dialogue, win)
hambox.pack_end(settings_button, False, False, False)

hambutton = Gtk.MenuButton(popover=hamburger)
hambutton_icon = ui.icon(win, "settings")
hambutton.add(hambutton_icon)
hambutton.set_relief(Gtk.ReliefStyle.NONE)
pannel.pack_end(hambutton)
hambox.show_all()

restofpannel = Gtk.HBox() # needed to be able to set sensitive true false
win.set_titlebar(pannel)#, False, False, False)

############## CONNECT BUTTON ################



# First we gonna check whether the SDK is running
win.SDK_running = connect.check()

# Connect and Disconnect boxes

icon_connect = ui.icon(win, "connect")
icon_disconnect = ui.icon(win, "disconnect")

connect_box = Gtk.HBox()
disconnect_box = Gtk.HBox()

connect_box.pack_start(icon_connect, False, False, False)
connect_box.set_tooltip_text("Connect to LBRY Network")
connect_box.pack_start(Gtk.Label("Connect "), False, False, False)

disconnect_box.pack_start(icon_disconnect, False, False, False)
disconnect_box.set_tooltip_text("Disconnect from LBRY Network")
disconnect_box.pack_start(Gtk.Label("Disconnect "), False, False, False)

if win.SDK_running:
    title = disconnect_box
    restofpannel.set_sensitive(True)
else:
    title = connect_box
    restofpannel.set_sensitive(False)


connect_disconncet_button = Gtk.Button()
connect_disconncet_button.add(title)
connect_disconncet_button.set_relief(Gtk.ReliefStyle.NONE)
pannel.pack_start(connect_disconncet_button)#, False, False, False)
pannel.pack_end(restofpannel)#, True, True, False)

def connect_disconnect_function(w):
    
    w.set_sensitive(False)
    
    if win.SDK_running:
        connect.stop()
        
    else:
        connect.start()
    win.SDK_running = not win.SDK_running
    w.get_child().destroy()
    if win.SDK_running:
        w.add(disconnect_box)
        restofpannel.set_sensitive(True)
    else:
        w.add(connect_box)
        restofpannel.set_sensitive(False)
    w.show_all()
    w.set_sensitive(True)

connect_disconncet_button.connect("clicked", connect_disconnect_function)

# A big search bar / lbry:// url bar



win.url = Gtk.Entry()
win.url.set_size_request(400,40)
restofpannel.pack_start(win.url, True, True, False)

# A search button
def search(w):
    if not force_search.get_active():
        win.url.set_text(parse.bar(win.url.get_text()))

        for i in dynamic_box.get_children():
            i.destroy()
        
        url.resolve(w, win, dynamic_box, win.url.get_text())
    
search_button = Gtk.Button()
search_icon = ui.icon(win, "search")
search_button.add(search_icon)
search_button.set_relief(Gtk.ReliefStyle.NONE)
search_button.connect("clicked", search)
win.url.connect("activate", search)
restofpannel.pack_start(search_button, False, False, False)

force_search = Gtk.CheckButton("Force Search")
force_search.set_tooltip_text("If not activated it will try to resolve the claim first.")
hambox.pack_start(force_search, False, False, False)
hambox.show_all()


########## SCROLLED WINDOW AND THE REST ###########


dynamic_box = Gtk.VBox()
scrl = Gtk.ScrolledWindow()
box.pack_start(scrl, True, True, False)
scrl.add_with_viewport(dynamic_box)

win.show_all()
Gtk.main()
